package com.jzc.rabbitmq.consumer;

import com.jzc.rabbit.common.MyRabbitMqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * RoutingKey模式（路由）-接收消息2
 *
 * @author zhichao.jiang01@hand-china.com 2022/10/24 9:57
 */
public class ConsumerRouting2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = MyRabbitMqUtils.getChannel();
        //4.创建Channel
        Channel channel =connection.createChannel();
        String queue2Name = "test_direct_queue2";
        /*
         basicConsume(String queue,boolean autoAck,Consumer consumer)
           1.queue: 队列名称。
           2.autoAck: 是否自动确认
           3.consumer：回调对象
         */
        //6.接收消息
        Consumer consumer =new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] body) {
                System.out.println("body:"+new String(body));
                System.out.println("将日志打印到控制台");
            }
        };
        channel.basicConsume(queue2Name,true,consumer);

    }

}
